<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class ProductSeckill extends Model
{
    protected $table = 'products_seckill';

    public function middle()
    {
        return $this->hasMany(ProductSecKillMiddleware::class, 'seckill_id','id');
    }

    public function product()
    {
        return $this->belongsToMany(Product::class, 'product_seckill_middle','seckill_id', 'product_id');
    }

    public function productTakeThree()
    {
        return $this->belongsToMany(
            Product::class,
            'product_seckill_middle',
            'seckill_id',
            'product_id')->take(3);
    }

    // 获取开启的秒杀产品
    public function scopeBegin($query)
    {
        return $query->where('start_at', '<', now())->where('end_at', '>', now());
    }

    public function getIsBeforeStartAttribute()
    {
        return Carbon::now()->lt($this->start_at);
    }

    public function getIsAfterEndAttribute()
    {
        return Carbon::now()->gt($this->end_at);
    }
}
